package com.sample.Calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CalculatorActivity extends Activity implements OnClickListener {
	String output;
	StringBuffer Output = new StringBuffer("");
	int operation = 0;
	int Number1 = 0;
	int Number2 = 0;
	boolean hasEnded = false;
	boolean hasEnded2 = false;
	Button[] Buttons = new Button[16];
	TextView text;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		text = (TextView) findViewById(R.id.textView1);
		Buttons[0] = (Button) findViewById(R.id.button1);
		Buttons[1] = (Button) findViewById(R.id.button2);
		Buttons[2] = (Button) findViewById(R.id.button3);
		Buttons[3] = (Button) findViewById(R.id.button4);
		Buttons[4] = (Button) findViewById(R.id.button5);
		Buttons[5] = (Button) findViewById(R.id.button6);
		Buttons[6] = (Button) findViewById(R.id.button7);
		Buttons[7] = (Button) findViewById(R.id.button8);
		Buttons[8] = (Button) findViewById(R.id.button9);
		Buttons[9] = (Button) findViewById(R.id.button10);
		Buttons[10] = (Button) findViewById(R.id.button11);
		Buttons[11] = (Button) findViewById(R.id.button12);
		Buttons[12] = (Button) findViewById(R.id.button13);
		Buttons[13] = (Button) findViewById(R.id.button14);
		Buttons[14] = (Button) findViewById(R.id.button15);
		Buttons[15] = (Button) findViewById(R.id.button16);
		Buttons[10].setText("+");
		Buttons[11].setText("-");
		Buttons[12].setText("*");
		Buttons[13].setText("/");
		Buttons[14].setText("=");
		Buttons[15].setText("Clr");
		for (int i = 0; i < 16; i++) {
			int value = +i;
			if (i < 10) {
				Buttons[i].setText(Integer.toString(value));
			}
			Buttons[i].setOnClickListener(this);

		}

	}

	public void onClick(View V) {
		for (int i = 0; i < 10; i++) {
			if (V.equals(Buttons[i])) {
				evaluate(i);
			}
		}
		if (V.equals(Buttons[10]) && operation == 0) {
			operation = 1;
			Output.append("+");
			text.setText(Output);
		} else if (V.equals(Buttons[11]) && operation == 0) {
			operation = 2;
			Output.append("-");
			text.setText(Output);
		} else if (V.equals(Buttons[12]) && operation == 0) {
			operation = 3;
			Output.append("*");
			text.setText(Output);
		} else if (V.equals(Buttons[13]) && operation == 0) {
			operation = 4;
			Output.append("/");
			text.setText(Output);
		} else if (V.equals(Buttons[14])) {
			if (operation != 0 && hasEnded == true && hasEnded2 == true) {
				Calculate();
				int tempNumber = Integer.parseInt(output);
				reset();
				Number1 = tempNumber;
				Output.append(Number1);
				hasEnded = true;
			} else if (operation == 0 && hasEnded == true && hasEnded2 == false) {
				text.setText(Integer.toString(Number1));
			} else {
				text.setText("Error");
				reset();
			}
		} else if (V.equals(Buttons[15])) {
			reset();
			text.setText("Calculator");
		}
	}

	public void evaluate(int value) {
		if (operation == 0) {
			Number1 = Number1 * 10 + value;
			hasEnded = true;
			Output.append(value);
			text.setText(Output);
		} else {
			Number2 = Number2 * 10 + value;
			hasEnded2 = true;
			Output.append(value);
			text.setText(Output);
		}

	}

	public void Calculate() {
		if (operation == 1) {
			output = Integer.toString(Number1 + Number2);
			text.setText(output);
		} else if (operation == 2) {
			output = Integer.toString(Number1 - Number2);
			text.setText(output);
		} else if (operation == 3) {
			output = Integer.toString(Number1 * Number2);
			text.setText(output);
		} else {
			try {
				output = Integer.toString(Number1 / Number2);
				text.setText(output);
			} catch (ArithmeticException e) {
				text.setText("Error: Division by Zero");
			}
		}
	}

	public void reset() {
		Output.delete(0, Output.length());
		output = "";
		operation = 0;
		Number1 = 0;
		Number2 = 0;
		hasEnded = false;
		hasEnded2 = false;
	}
}
